//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
// #define int long long
#define ull unsigned long long
#define lll __int128
//#define double long double
#define fi first
#define se second
using namespace std;
const long long INF=1e16;
//const long long mod=998244353,orm=3;
// const long long mod=1000000007;
const int MAXN=200005;
const double eps=1e-6;
bool ST;
vector<int> v[MAXN],qr[MAXN];
int n,Q,m,N;
int a[MAXN],prt[MAXN],sz[MAXN],sta[MAXN];
int p[MAXN][21];
struct o {
    int x,y,ans;
}q[MAXN];
inline int gf(int x) {
    if(prt[x]==x) return x;
    int top=0;
    while(prt[x]!=x) {
        sta[++top]=x;
        x=prt[x];
    }
    for(int i=1;i<=top;i++) prt[sta[i]]=x;
    return x;
}
int ask(int l,int r) {
    int k=__lg(r-l+1);
    return p[l][k]|p[r-(1<<k)+1][k];
}
void solve() {
    cin>>n>>Q>>m;N=(1<<m)-1;
    for(int i=1;i<=n;i++) cin>>a[i];
    int fg=1;
    for(int i=2,x;i<=n;i++) cin>>x,v[x].push_back(i),fg&=(x==(i-1));
    for(int i=1;i<=Q;i++) {
        int x,y;cin>>y>>x;q[i]={x,y};
        qr[y].push_back(i);
    }
    if(fg) {
        for(int i=1;i<=n;i++) p[i][0]=(1<<a[i]);
        for(int j=1;(1<<j)<=n;j++) {
            for(int i=1;i<=n;i++) p[i][j]=p[i][j-1]|(p[i+(1<<j-1)][j-1]);
        }
        for(int i=1;i<=Q;i++) {
            int x=q[i].x,y=q[i].y;
            if(!(y&(1<<a[x]))) q[i].ans=1;
            else {
                int l=1,r=x,res=x;
                while(l<=r) {
                    int mid=(l+r)>>1;
                    if((ask(mid,x)&y)==ask(mid,x)) res=mid,r=mid-1;
                    else l=mid+1;
                }
                l=x,r=n;int res2=x;
                while(l<=r) {
                    int mid=(l+r)>>1;
                    if((ask(x,mid)&y)==ask(x,mid)) res2=mid,l=mid+1;
                    else r=mid-1;
                }
                q[i].ans=res2-res+1;
            }
        }
    }
    else {
        for(int i=1;i<=N;i++) {
            for(int j=1;j<=n;j++) prt[j]=j,sz[j]=0;
            for(int j=1;j<=n;j++) {
                for(int k:v[j]) {
                    if((i&(1<<a[j]))&&(i&(1<<a[k]))) prt[gf(j)]=gf(k);
                }
            }
            for(int j=1;j<=n;j++) sz[gf(j)]++;
            for(int j:qr[i]) q[j].ans=sz[gf(q[j].x)];
        }
    }
    for(int i=1;i<=Q;i++) cout<<max(q[i].ans,1)<<"\n";
}
bool ED;
signed main() {
	// cout<<(&ST-&ED)/1024.0/1024.0;
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    solve();
	return 0;
}